<template>
  <div>
    <!--  面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
<!--      roleList区域-->
      <el-row>
        <el-table :data="roleList" border stripe>
<!--          expand为展开列-->
          <el-table-column type="expand">
            <template slot-scope="scope">
               <el-row :class="['bdbottom',childIndex === 0 ? 'bdtop' : '' ,'vcenter']" v-for="(childItem,childIndex) in scope.row.children" :key="childItem.id">
<!--                 一级权限-->
                 <el-col :span="5">
                   <el-tag>
                     {{childItem.authName}}
                   </el-tag>
                   <i class="el-icon-caret-right">
                   </i>
                 </el-col>
<!--                 二三级权限-->
                 <el-col :span="19">
                   <el-row :class="[index2 === 0 ? '' : 'bdtop','vcenter']" v-for="(item2,index2) in childItem.children" :key="item2.id">
<!--                     二级权限-->
                     <el-col :span="6">
                       <el-tag type="warning">
                         {{item2.authName}}
                       </el-tag>
                       <i class="el-icon-caret-right">
                       </i>
                     </el-col>
<!--                     三级权限-->
                         <el-col :span="18">
                           <el-tag closable type="success" v-for="item3 in item2.children" :key="item3.id" @close="removeRightsById()">
                             {{item3.authName}}
                           </el-tag>
                         </el-col>
                   </el-row>
                 </el-col>
               </el-row>
            </template>
          </el-table-column>
<!--          index为索引列-->
          <el-table-column type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName">

          </el-table-column>
          <el-table-column label="角色描述" prop="roleDesc">

          </el-table-column>
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <!--          修改按钮-->
              <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
              <!--          删除按钮-->
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              <el-button type="warning" icon="el-icon-setting" size="mini">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Roles",
  data(){
    return{
      roleList:[],
    }
  },
  created() {
    this.getRoleList()
  },
  methods:{
    async getRoleList(){
      const {data:res} = await this.$axios.get('roles')
      if(res.meta.status !==200 )
        return this.$message.error('请求角色列表失败')
      this.roleList = res.data
    },
    removeRightsById(){
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data:res} = await this.$axios.delete('users/'+id)
        if(res.meta.status !== 200)
          this.$message.error('删除用户失败')
        this.$message({
          type: 'success',
          message: '删除用户成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
  }
}
</script>

<style lang="less" scoped>
.el-tag{
  margin:7px;
}
.bdtop{
  border-top: 1px solid #eee;
}
.bdbottom{
  border-bottom: 1px solid #eee;
}
//设置居中
.vcenter{
  display:flex;
  align-items: center;
}
</style>